package com.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.domain.UserCollectionEntity;
import com.domain.Video;
import com.dto.PageResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mapper.UserCollectionMapper;
import com.service.UserCollectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 视频收藏记录表 服务实现类
 * </p>
 *
 * @author sly
 * @since 2024-01-13
 */
@Service
public class UserCollectionServiceImpl extends ServiceImpl<UserCollectionMapper, UserCollectionEntity> implements UserCollectionService {

    @Autowired
    private UserCollectionMapper userCollectionMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public PageResult<Video> pageListUserCollection(UserCollectionEntity userCollectionEntity) {
        PageHelper.startPage(userCollectionEntity.getCurrentPage(), userCollectionEntity.getCurrentSize());
        List<Video> userCollectionEntities = userCollectionMapper.getUserCollection(userCollectionEntity.getUserId());
        PageInfo<Video> pageInfo = new PageInfo<>(userCollectionEntities, userCollectionEntity.getCurrentSize());
        return new PageResult<>((int) pageInfo.getTotal(), pageInfo.getList());
    }

    @Override
    public void addUserCollection(UserCollectionEntity userCollectionEntity) {
        userCollectionEntity.setCreateTime(new Date());
        userCollectionMapper.insert(userCollectionEntity);
    }

    @Override
    public void deleteUserCollection(UserCollectionEntity userCollectionEntity) {
        QueryWrapper<UserCollectionEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(UserCollectionEntity::getUserId, userCollectionEntity.getUserId())
                .eq(UserCollectionEntity::getVideoId, userCollectionEntity.getVideoId());
        userCollectionMapper.delete(queryWrapper);
    }
}
